﻿using FoodModel;
using J.FoodManageGet.Application.Command.SpecificationCommand;
using J.FoodRepository.InterFace;
using MediatR;
using Microsoft.IdentityModel.Tokens;

namespace J.FoodManageGet.Application.CommandHandler.SpecificationCommandHandler
{
    /// <summary>
    /// 菜品规格显示
    /// </summary>
    public class SpecificationShowCommandHandler : IRequestHandler<SpecificationShowCommand, (int totalCount, int PagCount, List<SpecificationModel> list)>
    {
        private readonly ISpecificationRepository _specificationRepository;

        public SpecificationShowCommandHandler(ISpecificationRepository specificationRepository)
        {
            _specificationRepository = specificationRepository;
        }
        public async Task<(int totalCount, int PagCount, List<SpecificationModel> list)> Handle(SpecificationShowCommand request, CancellationToken cancellationToken)
        {
            var list = await _specificationRepository.SpecificationShow();
            if(request.SpecificationId!=0)
            {
                list = list.Where(x => x.SpecificationId== request.SpecificationId).ToList();
            }
            list.ForEach(specification =>
            {
                specification.Separates = specification.Separate == "" ? new List<string>() : specification.Separate.Split(',').ToList();
            });
            int PageTotal = list.Count;
            int PageCount = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize);
            list = list.OrderByDescending(x => x.SpecificationId).Skip((request.PageIndedx - 1) * request.PageSize).Take(request.PageSize).ToList();
            return (PageTotal, PageCount, list);
        }
    }
}
